<?php
/**
 * Created by PhpStorm.
 * User: 帅帅吴
 * Date: 2018/10/24
 * Time: 16:43
 */

namespace app\admin\controller;

use app\admin\model\PacketRecord;
use app\admin\model\RedPacket as RedPacketModel;
use app\admin\model\PacketRule as PacketRuleModel;
use app\admin\model\PacketRecord as PacketRecordModel;
class RedPacket extends Index
{
    /**
     * @return \think\response\View
     * 红包
     * 帅帅吴
     */
    public function index()
    {
        return view();
    }
    public function indexData()
    {

        $redPacketModel =new RedPacketModel();
        $data = $redPacketModel->getDataList();
        return json($data);
    }

    /**
     *
     * @title 速配红包规则
     */
    public function packetRuleIndex()
    {
        return view();
    }


    public function packetRuleIndexData()
    {
        $packetRuleModel = new PacketRuleModel();
        $data = $packetRuleModel->getDataList();
        return json($data);
    }

    public function packetRuleAdd()
    {
        $packetRuleModel = new PacketRuleModel();
        $res = $packetRuleModel->packetRuleAdd();
        if($res['code']==100) $this->success($res['msg']);
        else $this->error($res['msg']);

        return redirect('/Miss');
    }

    public function packetRuleChangeStatus()
    {
        $packetRuleModel = new PacketRuleModel();
        $res = $packetRuleModel->packetRuleChangeStatus();
        return $res;
    }

    public function packetRuleEdit()
    {
        $packetRuleModel = new PacketRuleModel();
        $res = $packetRuleModel->packetRuleEdit();
        return $res;
    }

    /**
     * @return \think\response\View
     * @title 速配红包记录
     */
    public function packetRecordIndex()
    {
        return view();
    }

    /**
     *
     */
    public function packetRecordIndexData()
    {
        $packetRecordModel = new PacketRecordModel();
        $data = $packetRecordModel->getDataList();
        return json($data);

    }

    public function packetRecordCountData()
    {
        $type = input('get.time');
        $start_time =strtotime(input('get.start_time'));
        $end_time = strtotime(input('get.end_time'));
        $key = input('get.key');
        $value = input('get.value');
        $where = [];
        if(!empty($type)){
            if(!empty($start_time) && !empty($end_time)){
                $where[] = [$type,'between',[$start_time,$end_time]];
            }
            if(!empty($start_time) && empty($end_time)){
                $where[] = [$type,'gt',$start_time];
            }
            if(empty($start_time) && !empty($end_time)){
                $where[] = [$type,'gt',$end_time];
            }
            if(!empty($key) && !empty($value)){

                $user_id = \app\admin\model\User::where('u_id','=',$value)->value('id');
                $where[] = [$key,'eq',$user_id];
            }
        }
        $data['record_count'] = PacketRecord::where($where)->count();  //红包数
        $data['diamonds_count'] = PacketRecord::where($where)->sum('diamonds'); //钻石数
        $where[] = ['status','eq',1];
        $data['success_count'] = PacketRecord::where($where)->sum('diamonds'); //已领取
        return ['code'=>100,'data'=>$data];
    }
}